<template>
  <div id="login">
    <div class="box">
      <el-form ref="form" :model="form" :rules="rule">
        <el-form-item>
          <div>
            <img src="@/assets/common/login-logo.png" alt="">
          </div>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input
            v-model="form.mobile"
            prefix-icon="el-icon-user-solid"
            @keyup.enter.native="submit"
          >登录</el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            type="password"
            @keyup.enter.native="submit"
          >密码</el-input>
        </el-form-item>
        <el-button style="width: 100%" @click="submit"> 登录 </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '13800000003',
        password: '123456'
      },
      rule: {
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '请输入6到12位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await this.$store.dispatch('user/toLogin', this.form)
          const _redirect = this.$route.query.redirect || ''
          this.$router.push(_redirect)
          // this.$message.success('登录成功')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
#login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url('../../assets/common/login.jpg') no-repeat center;
  .box {
    width: 550px;

    .el-button {
      background-color: #407ffe;
      color: #fff;
      border-width: 0;
    }
  }
}
</style>
